package com.syni.mdd.sjb.common.datatreaure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.common.entity.DxshAppUserInfo;
import com.syni.mdd.sjb.common.component.mybatis.common.mapper.DxshAppUserInfoMapper;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddUser.LoginDxshAppUserDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddUser.RegisterDxshAppUserDto;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 电享生活商户APP 个人资料信息 服务实现类
 * </p>
 *
 * @author lcc
 * @since 2020-04-27
 */
@Service
public class DxshAppUserInfoServiceImpl extends ServiceImpl<DxshAppUserInfoMapper, DxshAppUserInfo> implements DxshAppUserInfoService {

    @Autowired
    DxshAppUserInfoMapper dxshAppUserInfoMapper;

    @Override
    public DxshAppUserInfo syncDataTreasureUser(RegisterDxshAppUserDto registerDxshAppUserDto) {
        DxshAppUserInfo dxshAppUserInfo;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserInfo.USER_ID,registerDxshAppUserDto.getUserId());
        List<DxshAppUserInfo> dxshAppUserInfos = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(dxshAppUserInfos)){
            dxshAppUserInfo = dxshAppUserInfos.get(0);
            if(StringUtils.isNotBlank(registerDxshAppUserDto.getAccountId())){
                dxshAppUserInfo.setAccountId(registerDxshAppUserDto.getAccountId());
            }
            if(StringUtils.isNotBlank(registerDxshAppUserDto.getPhone())){
                dxshAppUserInfo.setPhone(registerDxshAppUserDto.getPhone());
            }
            dxshAppUserInfoMapper.updateById(dxshAppUserInfo);
        }else {
            dxshAppUserInfo = new DxshAppUserInfo();
            dxshAppUserInfo.setUserId(registerDxshAppUserDto.getUserId());
            dxshAppUserInfo.setSex(DxshAppUserInfo.SEX_DEFAULT);
            if(StringUtils.isNotBlank(registerDxshAppUserDto.getAccountId())){
                dxshAppUserInfo.setAccountId(registerDxshAppUserDto.getAccountId());
            }
            if(StringUtils.isNotBlank(registerDxshAppUserDto.getPhone())){
                dxshAppUserInfo.setPhone(registerDxshAppUserDto.getPhone());
            }
            dxshAppUserInfoMapper.insert(dxshAppUserInfo);
        }
        return dxshAppUserInfo;
    }

    public DxshAppUserInfo syncDataTreasureUser(LoginDxshAppUserDto loginDxshAppUserDto) {
        DxshAppUserInfo dxshAppUserInfo;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserInfo.USER_ID,loginDxshAppUserDto.getUserId());
        List<DxshAppUserInfo> dxshAppUserInfos = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(dxshAppUserInfos)){
            dxshAppUserInfo = dxshAppUserInfos.get(0);
            if(StringUtils.isNotBlank(loginDxshAppUserDto.getAccountId())){
                dxshAppUserInfo.setAccountId(loginDxshAppUserDto.getAccountId());
            }
            if(StringUtils.isNotBlank(loginDxshAppUserDto.getPhone())){
                dxshAppUserInfo.setPhone(loginDxshAppUserDto.getPhone());
            }
            dxshAppUserInfoMapper.updateById(dxshAppUserInfo);
        }else {
            dxshAppUserInfo = new DxshAppUserInfo();
            dxshAppUserInfo.setUserId(loginDxshAppUserDto.getUserId());
            dxshAppUserInfo.setSex(DxshAppUserInfo.SEX_DEFAULT);
            if(StringUtils.isNotBlank(loginDxshAppUserDto.getAccountId())){
                dxshAppUserInfo.setAccountId(loginDxshAppUserDto.getAccountId());
            }
            if(StringUtils.isNotBlank(loginDxshAppUserDto.getPhone())){
                dxshAppUserInfo.setPhone(loginDxshAppUserDto.getPhone());
            }
            dxshAppUserInfoMapper.insert(dxshAppUserInfo);
        }
        return dxshAppUserInfo;
    }

    public DxshAppUserInfo getByUserId(Integer userId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserInfo.USER_ID,userId);
        List<DxshAppUserInfo> dxshAppUserInfos = dxshAppUserInfoMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(dxshAppUserInfos)){
            return dxshAppUserInfos.get(0);
        }
        return null;
    }

    @Override
    public DxshAppUserInfo getByUnionId(String unionId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserInfo.UNION_ID,unionId);
        List<DxshAppUserInfo> dxshAppUserInfos = dxshAppUserInfoMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(dxshAppUserInfos)){
            return dxshAppUserInfos.get(0);
        }
        return null;
    }
}
